package dao.impl;
/**
* @author	gem
* UserDAO实现
*/
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import dao.UserDAO;
import po.Book;
import po.User;

public class UserDAOImpl implements UserDAO {
	private Connection con = null;
	
	public UserDAOImpl(Connection con) {
		this.con=con;
	}
	@Override
	public boolean addUser(User newUser) {
		// ���û���������µ��û�
		String userId = checkNull(newUser.getUserId());
		String userPassword = checkNull(newUser.getUserPassword());
		String userName = checkNull(newUser.getUserName());
		String gender = checkNull(newUser.getUserGender());
		int commentNum = newUser.getCommentNum();
		String motto = checkNull(newUser.getMotto());
		String imagePath = checkNull(newUser.getUserImagePath());
		String sql = "insert into darkcirclenovel.userinformation values(?,?,?,?,?,?,?)";
		PreparedStatement sta = null;
		int result = -1;
		try {
			sta = this.con.prepareStatement(sql);
			sta.setString(1,userId);
			sta.setString(2,userPassword);
			sta.setString(3,userName);
			sta.setString(4,gender);
			sta.setInt(5, commentNum);
			sta.setString(6,motto);
			sta.setString(7,imagePath);

			result = sta.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result==1?true:false;
	}

	@Override
//	�����˻�������е�¼���鿴�Ƿ��˺������������ݿ��д��ڣ�
	public boolean LoginIn(String userId, String password) {
		String sql = "select * from darkcirclenovel.userinformation where userId = ? and password = ?;";
		PreparedStatement sta = null;
		int rows = -1;
		try {
			sta = this.con.prepareStatement(sql);
			sta.setString(1,userId);
			sta.setString(2,password);
			ResultSet res = sta.executeQuery();
			res.last();
			rows = res.getRow();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rows>=0?true:false;
	}

	@Override
	public List<User> getUserById(String userId) {
		String sql = "select * from darkcirclenovel.userinformation where userId = ?";
		PreparedStatement sta = null;
		List<User> result = new ArrayList<>();
		try {
			sta = this.con.prepareStatement(sql);
			sta.setString(1,userId);
			ResultSet res = sta.executeQuery();
			result = resultToUserList(res);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public boolean updateUserInformation(User newUser) {

		String userId = checkNull(newUser.getUserId());
		String userPassword = checkNull(newUser.getUserPassword());
		String userName = checkNull(newUser.getUserName());
		String gender = checkNull(newUser.getUserGender());
		int commentNum = newUser.getCommentNum();
		String motto = checkNull(newUser.getMotto());
		String imagePath = checkNull(newUser.getUserImagePath());
		
		String sql = "update darkcirclenovel.userinformation set userId = ?,password = ?,nickname=?,gender = ?,commentNum =?,motto =?,userImagePath=? where userid = ?;" ;
		PreparedStatement sta = null;
		int result = -1;
		try {
			sta = this.con.prepareStatement(sql);
			sta.setString(1,userId);
			sta.setString(2,userPassword);
			sta.setString(3,userName);
			sta.setString(4,gender);
			sta.setInt(5, commentNum);
			sta.setString(6,motto);
			sta.setString(7,imagePath);
			sta.setString(8,userId);
			
			result = sta.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result==1?true:false;
	}

	@Override
	public List<User> getUserByName(String username) {
		String sql = "select * from darkcirclenovel.userinformation where nickname = ?";
		PreparedStatement sta = null;
		List<User> result = new ArrayList<>();
		try {
			sta = this.con.prepareStatement(sql);
			sta.setString(1,username);
			ResultSet res = sta.executeQuery();
			result = resultToUserList(res);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	public String checkNull(String origin) {
		if(origin==null) {
			return "";
		}
		else {
			return origin;
		}
	}
	public List<User> resultToUserList(ResultSet res ) {
		List<User> resu = new ArrayList<>();
		try {
			while(res.next()) {
				String userId = res.getString("userId");
				String password = res.getString("password");
				String nickName = res.getString("nickname");
				String gender = res.getString("gender");
				
				int count = res.getInt("commentNum");
				String motto = res.getString("motto");
				String image = res.getString("userImagePath");
				User newUser = new User(userId,password,nickName,gender,count,motto,image);
				resu.add(newUser);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resu;
		
	}
}
